#!/bin/sh
# $Revision$
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# (c) Tim Brown, 2012
# <mailto:timb@nth-dimension.org.uk>
# <http://www.nth-dimension.org.uk/> / <http://www.machine.org.uk/>
#
# Supports: AIX, Solaris, Linux

if [ -z "${parseincluded}" ]
then

parseincluded=1

. lib/misc/file
. lib/misc/validate

parse_extract_absolute_filepaths () {
	pattern="${1}"
	if [ -z "${pattern}" ]
	then
		printf -- ""
	fi
	printf -- "${pattern}" | egrep -o "/[a-z|A-Z|0-9|/|-|_|.]*" | while read filepath
	do
		printf -- "${filepath}\n"
	done
}

parse_environ_cwd () {
	pid="${1}"
	[ "`validate_is_number \"${pid}\"`" ] || false
	if [ "`file_is_readable \"/proc/${pid}/environ\"`" -eq 1 ]
	then
		# the tail is because /proc/PID/environ contains multiple PWD values, only the last is the current working directory
		# the cut is to strip the 'PWD=' from the grepped pattern
		printf -- "`egrep -a -o \"PWD=/[a-z|A-Z|0-9|/|-|_|.]*\" \"/proc/${pid}/environ\" | tail -1 | cut -c5-`\n"
	fi
}

fi
